<template>
  <view>
    <view class="w100 pl-60 pr-60">
      <view class="w100 h-124 rows rowsm" style="border-bottom: 1rpx solid rgba(0, 0, 0, .06);">
        <input type="text" password placeholder="请输入旧密码" placeholder-class="fs-30 col888" v-model="pwd">
      </view>
      <view class="w100 h-124 rows rowsm" style="border-bottom: 1rpx solid rgba(0, 0, 0, .06);">
        <input type="text" password placeholder="请输入新密码" placeholder-class="fs-30 col888" v-model="newPwd">
      </view>
      <view class="w100 h-124 rows rowsm" style="border-bottom: 1rpx solid rgba(0, 0, 0, .06);">
        <input type="text" password placeholder="确认密码" placeholder-class="fs-30 col888" v-model="newPwd2">
      </view>

      <view class="w100 mt-192 rowsc rowsm">
        <view class="w100 h-100 br-24 rowsc rowsm colfff fs-32 fw-b"
          style="background: linear-gradient(to bottom,#4897FF,#4878FF);" @click="changePwd()">确定</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      pwd: '',
      newPwd: '',
      newPwd2: '',
      flag: '',
    };
  },
  onLoad() {
    this.flag = uni.getStorageSync('flag');
  },
  methods: {
    changePwd() {
      if (!this.pwd) return this.$tools.showtt('请输入密码')
      if (!this.newPwd) return this.$tools.showtt('请输入新密码')
      // 密码长度在5-20位之间
      if (this.newPwd.length < 5 || this.newPwd.length > 20) return this.$tools.showtt('密码长度在5-20位之间')
      if (this.newPwd2.length < 5 || this.newPwd2.length > 20) return this.$tools.showtt('密码长度在5-20位之间')
      if (this.newPwd != this.newPwd2) return this.$tools.showtt('两次密码不一致')
      if (this.flag == 'student') {
        let data1 = {
          password: this.pwd,
          passwordNew: this.newPwd
        }
        this.$tools.axiosFromToken('POST', 'prouser/updatePassword', { ...data1 }, '加载中').then(res => {
          this.$tools.showtt(res.msg)
          if (res.code != 200) return
          setTimeout(() => {
            // this.$tools.goBack(1)

            uni.removeStorageSync('flag');
            uni.removeStorageSync('getlnfo');
            uni.removeStorageSync('studentUnreport');
            uni.removeStorageSync('token');
            // window.location.href = "http://my.lyvc.edu.cn/cas/logout?service=http://portal.lyvc.edu.cn/";
            uni.reLaunch({
              url: '/pages/login/login'
            })
          }, 1000)
        })
      } else {
        let data2 = {
          oldPassword: this.pwd,
          newPassword: this.newPwd
        }
        this.$tools.axiosFromToken('put', 'system/user/profile/updatePwd', { ...data2 }, '加载中').then(res => {
          this.$tools.showtt(res.msg)
          if (res.code != 200) return
          setTimeout(() => {
            // this.$tools.goBack(1)

            uni.removeStorageSync('flag');
            uni.removeStorageSync('getlnfo');
            uni.removeStorageSync('studentUnreport');
            uni.removeStorageSync('token');
            // window.location.href = "http://my.lyvc.edu.cn/cas/logout?service=http://portal.lyvc.edu.cn/";
            uni.reLaunch({
              url: '/pages/login/login'
            })
          }, 1000)
        })
      }
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #fff;
}
</style>
